from rest_framework import serializers

from datasource.models import PictureList
from datasource.views.picture_type_list import PictureTypeSerializer
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.viewset import CustomModelViewSet


class PictureSerializer(CustomModelSerializer):
    type = PictureTypeSerializer
    class Meta:
        model = PictureList
        fields = "__all__"

    def create(self, validated_data):
        print(validated_data)
        return super().create(validated_data)


class PictureViewSet(CustomModelViewSet):
    """
    图片管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = PictureList.objects.all()
    def get_queryset(self):
        type_id = self.request.query_params.get('typeid')
        if(type_id == None):
            self.queryset = PictureList.objects.all()
        else:
            self.queryset = PictureList.objects.filter(type=type_id)
        return self.queryset
    serializer_class = PictureSerializer
    filter_fields = ['name', ]
    permission_classes = []
